layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/business/coupon_activity/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
       {field: 'id', title: '活动ID'}
      ,{field: 'title', title: '活动标题'}
      ,{field: 'userName', title: '活动所属人',templet:function (record){
          if(record.userName) {
              return record.userName;
          }else{
              return '平台';
          }
              }}
      ,{field: 'typeName', title: '活动类型'}
      ,{field: 'couponTypeName', title: '优惠类型'}
      ,{field: 'couponInfo', title: '优惠信息'}
      ,{field: 'useLimitName', title: '类型限制',templet:function (record){
          if(record.useLimit == 2){
              return record.businessTypeName;
          }else{
              return record.useLimitName;
          }
              }}
      ,{field: 'useRangeName', title: '使用范围'}
      ,{field: 'stateName', title: '状态'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 240, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
       ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      add:function (){
          layer.open({
              type: 2
              , title: '新增优惠活动'
              , content: layui.setter.webRoot + '/business/coupon_activity/add.html'
              , maxmin: true
              , area: ['80%', '600px']
              ,btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/business/coupon_activity/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          })
      }
      ,update:function (obj){
          layer.open({
              type: 2
              , title: '修改优惠活动'
              , content: layui.setter.webRoot + '/business/coupon_activity/update.html?id='+obj.data.id
              , maxmin: true
              , area: ['80%', '600px']
              ,btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/business/coupon_activity/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          })
      }
      ,info:function(obj){
          layer.open({
              type: 2
              ,title: '活动信息'
              ,content: layui.setter.webRoot + '/business/coupon_activity/info.html?id='+obj.data.id
              ,maxmin: true
              ,area: ['90%', '90%']
              ,btn: ['取消']
          });
      }
      , qrcode: function (obj) {
          layer.open({
              type: 2
              , title: '活动二维码'
              , content: layui.setter.webRoot + '/business/coupon_activity/qrcode.html?activityId=' + obj.data.id
              , maxmin: false
              , area: ['400px', '450px']
              , btn: ['取消']
          });
      }
     ,del:function(obj){
    	layer.confirm('确定要删除该优惠活动吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/business/coupon_activity/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
     }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('coupon_activity_list', {})
});